/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Diagnostico_View_Add.java
 *
 * Created on 14/08/2012, 11:43:06 PM
 */
package v;

import beans.DiscapacidadTratamiento;
import beans.MedicamentoDiscapacidad;
import beans.TerapiaDiscapacidad;
import beans.Tratamiento;
import beans.dao.CitaDAO;
import beans.dao.TratamientoDAO;
import core.constructor.constructor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
import tools.DatosComboBox;

/**
 *
 * @author Usuario
 */
public class Diagnostico_View_Add extends javax.swing.JPanel {

    private Integer idPaciente;
    private Integer idMedico;
    private Tratamiento tratamiento = new Tratamiento();
    private DefaultListModel discModel =
            new DefaultListModel();
    private TableModel terapiaModel;
    private TableModel medicamentoModel;
    //
    private String[] columnTerapia = {"Terapia", "Intensidad", "Cantidad", "Inicio"};
    private String[] columnMedic = {"Medicamento", "Dosis", "Uso", "Inicio"};
    private DatosComboBox cbx = new DatosComboBox();
    private Medicamento_View_Add medicamentoPanel;
    private Terapia_View_Add terapiaPanel;
    private Map<Integer, TerapiaDiscapacidad> terapiasTabla =
            new HashMap<Integer, TerapiaDiscapacidad>();
    private Map<Integer, MedicamentoDiscapacidad> medicamentosTabla =
            new HashMap<Integer, MedicamentoDiscapacidad>();
    private DiscapacidadTratamiento discapacidadSeleccionada;
    private constructor c;
    private JDialog test;
    private String idCitas;

    /**
     * Creates new form Diagnostico_View_Add
     */
    public Diagnostico_View_Add(constructor cons, Integer idPaciente, Integer idMedico,
            String idCitas) {
        initComponents();



        this.idPaciente = idPaciente;
        this.idMedico = idMedico;
        this.c = cons;
        this.idCitas = idCitas;

        setComponents();
        cargarCombos();

        setListeners();

        AutoCompleteDecorator.decorate(id_discapacidad);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel7 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        fecha_evaluacion = new org.jdesktop.swingx.JXDatePicker();
        medicamento_View_Add1 = new v.Medicamento_View_Add();
        terapia_View_Add1 = new v.Terapia_View_Add();
        botonAgregarMed = new javax.swing.JButton();
        botonAgregarTer = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tablaTerapias = new org.jdesktop.swingx.JXTable();
        jLabel3 = new javax.swing.JLabel();
        bRemTerapia = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        id_discapacidad = new javax.swing.JComboBox();
        addDiscapacidad = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        discapacidades = new javax.swing.JList();
        removeDisc = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        tablaMedicamentos = new org.jdesktop.swingx.JXTable();
        jLabel4 = new javax.swing.JLabel();
        bRemovMedicam = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        nota = new javax.swing.JTextArea();
        bGuardarTrat = new javax.swing.JButton();

        jLabel2.setText("Fecha");

        fecha_evaluacion.setFormats("dd-MM-yyyy");
        fecha_evaluacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fecha_evaluacionActionPerformed(evt);
            }
        });

        botonAgregarMed.setIcon(new javax.swing.ImageIcon(getClass().getResource("/core/img/add24.png"))); // NOI18N
        botonAgregarMed.setText("Agregar");
        botonAgregarMed.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAgregarMedActionPerformed(evt);
            }
        });

        botonAgregarTer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/core/img/add24.png"))); // NOI18N
        botonAgregarTer.setText("Agregar");
        botonAgregarTer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAgregarTerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(fecha_evaluacion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(terapia_View_Add1, javax.swing.GroupLayout.PREFERRED_SIZE, 366, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(botonAgregarTer, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(medicamento_View_Add1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(botonAgregarMed, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(fecha_evaluacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(medicamento_View_Add1, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(terapia_View_Add1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonAgregarMed)
                    .addComponent(botonAgregarTer))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tablaTerapias.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tablaTerapias);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel3.setText("Terapias");

        bRemTerapia.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        bRemTerapia.setIcon(new javax.swing.ImageIcon(getClass().getResource("/core/img/remove24.png"))); // NOI18N
        bRemTerapia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bRemTerapiaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 441, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(80, 80, 80)
                        .addComponent(bRemTerapia)))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3))
                    .addComponent(bRemTerapia))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        id_discapacidad.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        addDiscapacidad.setIcon(new javax.swing.ImageIcon(getClass().getResource("/core/img/add24.png"))); // NOI18N
        addDiscapacidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addDiscapacidadActionPerformed(evt);
            }
        });

        discapacidades.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        discapacidades.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                discapacidadesValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(discapacidades);

        removeDisc.setIcon(new javax.swing.ImageIcon(getClass().getResource("/core/img/remove24.png"))); // NOI18N
        removeDisc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeDiscActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Discapacidades:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(174, 174, 174))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(id_discapacidad, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(87, 87, 87))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(addDiscapacidad)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removeDisc)
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(id_discapacidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(addDiscapacidad)
                    .addComponent(removeDisc))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tablaMedicamentos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane4.setViewportView(tablaMedicamentos);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel4.setText("Medicamentos");

        bRemovMedicam.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        bRemovMedicam.setIcon(new javax.swing.ImageIcon(getClass().getResource("/core/img/remove24.png"))); // NOI18N
        bRemovMedicam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bRemovMedicamActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 463, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(bRemovMedicam)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel4))
                    .addComponent(bRemovMedicam))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel5.setText("Detalle diagnostico:");

        nota.setColumns(20);
        nota.setLineWrap(true);
        nota.setRows(5);
        jScrollPane3.setViewportView(nota);

        bGuardarTrat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/core/img/guardar32.png"))); // NOI18N
        bGuardarTrat.setText("Guardar");
        bGuardarTrat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bGuardarTratActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 1004, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addGroup(jPanel7Layout.createSequentialGroup()
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel7Layout.createSequentialGroup()
                                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(4, 4, 4)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(bGuardarTrat)
                .addGap(431, 431, 431))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5))
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bGuardarTrat))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    public void setTest(JDialog k) {
        test = k;
    }

private void removeDiscActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeDiscActionPerformed
    int index = discapacidades.getSelectedIndex();
    if (index >= 0) {
        Object discapacidad = discModel.get(index);
        tratamiento.getDispacacidadTratamientos().remove(discapacidad);
        discModel.remove(index);
        setTerapiaModel(new HashSet<TerapiaDiscapacidad>());
        setMedicamentosModel(new HashSet<MedicamentoDiscapacidad>());
        if (discModel.isEmpty()) {
            botonAgregarMed.setEnabled(false);
            botonAgregarTer.setEnabled(false);
        }

    }
}//GEN-LAST:event_removeDiscActionPerformed

private void discapacidadesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_discapacidadesValueChanged

    int index = discapacidades.getSelectedIndex();
    if (index >= 0) {
        discapacidadSeleccionada =
                (DiscapacidadTratamiento) discModel.get(index);
        setMedicamentosModel(discapacidadSeleccionada.getMedicamentosDiscapacidad());
        setTerapiaModel(discapacidadSeleccionada.getTerapiasDiscapacidad());
        terapiaPanel.resetearComponentes();
        medicamentoPanel.resetearComponentes();
    } else {
        discapacidadSeleccionada = null;
    }
}//GEN-LAST:event_discapacidadesValueChanged

private void bGuardarTratActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bGuardarTratActionPerformed
// TODO add your handling code here:

    try {
        if (tratamiento.getDispacacidadTratamientos().isEmpty()) {
            throw new Exception("No ha agregado ninguna discapacidad al diagnostico.");
        }
        for (DiscapacidadTratamiento d : tratamiento.getDispacacidadTratamientos()) {
            if (d.getMedicamentosDiscapacidad().isEmpty() && d.getTerapiasDiscapacidad().isEmpty()) {
                throw new Exception(String.format(
                        "Debe agregar por lo menos una terapia y/o medicamento "
                        + "para la discapacidad %s", d.getDiscapacidad()));
            }
        }
        
        tratamiento.setNota(nota.getText());
        tratamiento.setIdMedico(idMedico);
        tratamiento.setIdPaciente(idPaciente);
        tratamiento.setFechaEvaluacion(fecha_evaluacion.getDate());
        TratamientoDAO.guardarItem(tratamiento);
        c.informarUsuario("ok[Inserción de tratamiento]  Se guardo "
                + "correctamente la información");
        CitaDAO.cambiarEstadoCitaAsistido(idCitas);
        bGuardarTrat.setEnabled(false);
    } catch (Exception ex) {
        ex.printStackTrace();
        c.informarUsuario(String.format(
        "error[No se pudo guardar tratamiento] %s", ex.getMessage()));
    }
}//GEN-LAST:event_bGuardarTratActionPerformed

private void botonAgregarTerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAgregarTerActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_botonAgregarTerActionPerformed

private void addDiscapacidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addDiscapacidadActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_addDiscapacidadActionPerformed

private void botonAgregarMedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAgregarMedActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_botonAgregarMedActionPerformed

private void bRemTerapiaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bRemTerapiaActionPerformed

    int selectedRow = tablaTerapias.getSelectedRow();
    if (selectedRow > -1 && discapacidadSeleccionada != null) {
        TerapiaDiscapacidad terapiaBorrar = terapiasTabla.get(selectedRow);
        if (discapacidadSeleccionada.getTerapiasDiscapacidad().remove(terapiaBorrar)) {
            DefaultTableModel model = ((DefaultTableModel) tablaTerapias.getModel());
            model.removeRow(selectedRow);
        }
    }

}//GEN-LAST:event_bRemTerapiaActionPerformed

private void bRemovMedicamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bRemovMedicamActionPerformed
// TODO add your handling code here:
    int selectedRow = tablaMedicamentos.getSelectedRow();
    if (selectedRow > -1 && discapacidadSeleccionada != null) {
        MedicamentoDiscapacidad medicBorrar = medicamentosTabla.get(selectedRow);
        if (discapacidadSeleccionada.getMedicamentosDiscapacidad().remove(medicBorrar)) {
            DefaultTableModel model = ((DefaultTableModel) tablaMedicamentos.getModel());
            model.removeRow(selectedRow);
        }
    }
}//GEN-LAST:event_bRemovMedicamActionPerformed

    private void fecha_evaluacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fecha_evaluacionActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_fecha_evaluacionActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addDiscapacidad;
    private javax.swing.JButton bGuardarTrat;
    private javax.swing.JButton bRemTerapia;
    private javax.swing.JButton bRemovMedicam;
    private javax.swing.JButton botonAgregarMed;
    private javax.swing.JButton botonAgregarTer;
    private javax.swing.JList discapacidades;
    private org.jdesktop.swingx.JXDatePicker fecha_evaluacion;
    private javax.swing.JComboBox id_discapacidad;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private v.Medicamento_View_Add medicamento_View_Add1;
    private javax.swing.JTextArea nota;
    private javax.swing.JButton removeDisc;
    private org.jdesktop.swingx.JXTable tablaMedicamentos;
    private org.jdesktop.swingx.JXTable tablaTerapias;
    private v.Terapia_View_Add terapia_View_Add1;
    // End of variables declaration//GEN-END:variables

    private void cargarCombos() {
        id_discapacidad.setModel(new DefaultComboBoxModel(cbx.cbDiscapacidad()));
        id_discapacidad.setSelectedIndex(0);

//        DefaultComboBoxModel defaultComboBoxModel = new DefaultComboBoxModel();
//        tipoTratamiento.setModel(defaultComboBoxModel);
//        
//        defaultComboBoxModel.addElement("Medicamento");
//        defaultComboBoxModel.addElement("Terapia");
//        tipoTratamiento.setSelectedIndex(0);

        discapacidades.setModel(discModel);
    }

    private void setListeners() {
        addDiscapacidad.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object selected = id_discapacidad.getSelectedItem();
                if (null != selected) {
                    String selectedItem = selected.toString();
                    System.out.println("Selected: " + selectedItem);
                    Integer idDisca = Integer.valueOf(cbx.getKeyDiscapacidad(id_discapacidad));
                    System.out.println("Id: " + idDisca);
                    DiscapacidadTratamiento discapacidad = new DiscapacidadTratamiento();
                    discapacidad.setIdDiscapacidad(idDisca);
                    discapacidad.setDiscapacidad(selectedItem);

                    if (!tratamiento.getDispacacidadTratamientos().contains(discapacidad)) {
                        discModel.addElement(discapacidad);
                        discapacidades.setSelectedIndex(discModel.getSize() - 1);
                        tratamiento.getDispacacidadTratamientos().add(discapacidad);
                        botonAgregarMed.setEnabled(true);
                        botonAgregarTer.setEnabled(true);
                    }

                }
            }
        });

        botonAgregarTer.addActionListener(agregarTerapiaListener);
        botonAgregarMed.addActionListener(agregarMedicamentoListener);

    }
    //cada %s medida en dosis medida

    private void setTerapiaModel(Set<TerapiaDiscapacidad> terapias) {
        int size = terapias.size();
        Object[][] datos = new Object[size][columnTerapia.length];
        terapiasTabla = new HashMap<Integer, TerapiaDiscapacidad>();

        TerapiaDiscapacidad[] toArray = terapias.toArray(new TerapiaDiscapacidad[size]);

        for (int i = 0; i < toArray.length; i++) {
            final TerapiaDiscapacidad terapia = toArray[i];
            terapiasTabla.put(i, terapia);
            datos[i][0] = cbx.getValueTerapia(terapia.getIdTerapia().toString());
            System.out.println("Terapia: " + terapia.getIdTerapia().toString());
            datos[i][1] = String.format("Cada %s %s", terapia.getIntensidad(),
                    cbx.getValueMedidas(terapia.getIntensidadMedidaTiempo().toString()));
//            datos[i][2] = cbx.getValueMedidas(terapia.getIntensidadMedidaTiempo().toString());
//            System.out.println("Intensidad medida: " + terapia.getIntensidadMedidaTiempo().toString());
            datos[i][2] = terapia.getCantidad();
            datos[i][3] = terapia.getFechaInicio();
        }


        terapiaModel = new DefaultTableModel(datos, columnTerapia);
        tablaTerapias.setModel(terapiaModel);
    }

    private void setMedicamentosModel(Set<MedicamentoDiscapacidad> medicamentos) {
        int size = medicamentos.size();
        Object[][] datos = new Object[size][columnMedic.length];

        MedicamentoDiscapacidad[] toArray =
                medicamentos.toArray(new MedicamentoDiscapacidad[size]);

        medicamentosTabla = new HashMap<Integer, MedicamentoDiscapacidad>();


        for (int i = 0; i < toArray.length; i++) {
            final MedicamentoDiscapacidad medicamento = toArray[i];

            medicamentosTabla.put(i, medicamento);

            datos[i][0] = cbx.getValueMedicamentos(medicamento.getIdMedicamento().toString());
            datos[i][1] = String.format("Cada %s %s", medicamento.getDosis(),
                    cbx.getValueMedidas(medicamento.getUsoMedidaTiempo().toString()));
//            datos[i][2] = cbx.getValueMedidas(medicamento.getUsoMedidaTiempo().toString());
            datos[i][2] = String.format("Cada %s %s", medicamento.getUso(),
                    cbx.getValueMedidas(medicamento.getUsoMedidaTiempo().toString()));
//            datos[i][3] = cbx.getValueMedidas(medicamento.getUsoMedidaTiempo().toString());
            datos[i][3] = medicamento.getFechaInicio();
        }

        medicamentoModel = new DefaultTableModel(datos, columnMedic);
        tablaMedicamentos.setModel(medicamentoModel);
    }
    private ActionListener agregarTerapiaListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                TerapiaDiscapacidad info = terapiaPanel.getInfo();
                int index = discapacidades.getSelectedIndex();
                if (index >= 0) {
                    Object dis = discModel.get(index);
                    DiscapacidadTratamiento trat = (DiscapacidadTratamiento) dis;
                    info.setFechaInicio(fecha_evaluacion.getDate());
                    trat.getTerapiasDiscapacidad().add(info);
                    setTerapiaModel(trat.getTerapiasDiscapacidad());
                    terapiaPanel.resetearComponentes();
//                    vistasPanel.removeAll();

//                    botonAgregarTer.removeActionListener(agregarTerapiaListener);
                }
            } catch (Exception ex) {
                c.informarUsuario(String.format(
                        "error[Al agregar terapia] %s", ex.getMessage()));
                Logger.getLogger(Diagnostico_View_Add.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    };
    private ActionListener agregarMedicamentoListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                MedicamentoDiscapacidad info = medicamentoPanel.getInfo();
                int index = discapacidades.getSelectedIndex();
                if (index >= 0) {
                    Object dis = discModel.get(index);
                    info.setFechaInicio(fecha_evaluacion.getDate());
                    DiscapacidadTratamiento trat = (DiscapacidadTratamiento) dis;
                    trat.getMedicamentosDiscapacidad().add(info);
                    setMedicamentosModel(trat.getMedicamentosDiscapacidad());

                    medicamentoPanel.resetearComponentes();
//                    vistasPanel.removeAll();

//                    botonAgregarTer.removeActionListener(agregarMedicamentoListener);
                }
            } catch (Exception ex) {
                c.informarUsuario(String.format(
                        "error[Al agregar medicamento] %s", ex.getMessage()));
                Logger.getLogger(Diagnostico_View_Add.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    };

    private void setComponents() {
        fecha_evaluacion.setDate(new Date());
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        JFormattedTextField dateEditor = new JFormattedTextField(df);
        fecha_evaluacion.setEditor(dateEditor);


        botonAgregarTer.setEnabled(false);
        botonAgregarMed.setEnabled(false);

        terapiaPanel = terapia_View_Add1;

        medicamentoPanel = medicamento_View_Add1;

        c.setLabelModulo("Valoración");
        c.setLabelAccion("Valoración inicial Paciente");
    }
}
